/**
 * JAVACC DEMO 1.0
 */
package com.sale.taskplugins;

import com.apache.api.api.TimeTaskService;
import com.apache.api.manager.PluginConnector;
import com.apache.api.manager.ProxyManager;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.cache.util.Validator;
import com.apache.database.db.IDao;
import com.apache.ius.CustomMethodFactory;
import com.apache.ius.plugin.CustomMethodPlugin;
import org.apache.log4j.Logger;

import java.util.Map;

/**
 * 【闭市撤单】每天闭市对未成交订单进行撤单
 *
 * @author Administrator创建时间：2017-1-12
 */
public class CancellationsTaskPluginImpl implements PluginConnector, TimeTaskService {

    private ProxyManager netProxyManager;

    private IDao trpFeeDao;

    private transient static boolean CancellationsTask = true;

    private Logger log = Logger.getLogger(getClass());

    /**
     * TODO 简单描述该方法的实现功能（可选）.
     *
     * @see com.apache.api.manager.PluginConnector#execute(com.apache.api.vo.ParamsVo)
     */
    public Object execute(ParamsVo arg0) throws Exception {
        exeCancellations();
        return new ResultEntity();
    }

    public boolean run() {
        exeCancellations();
        return true;
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    private void exeCancellations() {
        try {
            if (CancellationsTask) {
                CancellationsTask = false;
                ParamsVo vo = new ParamsVo();
                vo.setKey("cancellations");
                Object obj = netProxyManager.saveInvoke(vo);//调用批量撤单插件
                log.info(obj);
                if (!Validator.isEmpty(obj)) {
                    Map<String, String> map = (Map) obj;
                    CustomMethodPlugin plugin = CustomMethodFactory.getInstance()
                            .getPluginByKey("saleQuotationUpdate");
                    for (String key : map.keySet()) {//调用行情
                        vo.setParams("trsOrder.proNo", map.get(key));
                        plugin.afterInvoke(vo.getParams());
                    }
                }
            }
        } finally {
            CancellationsTask = true;
        }
    }

    public void setNetProxyManager(ProxyManager netProxyManager) {
        this.netProxyManager = netProxyManager;
    }

    public void setTrpFeeDao(IDao trpFeeDao) {
        this.trpFeeDao = trpFeeDao;
    }

}
